package com.cowain.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cowain.monitor.dao.ErpNorDerDao;
import com.cowain.monitor.dao.ErpSoDao;
import com.cowain.monitor.dao.PmsOrderInfoDao;
import com.cowain.monitor.datasources.DataSourceNames;
import com.cowain.monitor.datasources.annotation.DataSource;
import com.cowain.monitor.datasources.config.TransactionConfig;
import com.cowain.monitor.entity.ErpNorderEntity;
import com.cowain.monitor.entity.ErpSoEntity;
import com.cowain.monitor.entity.PmsOrderInfoEntity;
import com.cowain.monitor.service.ErpNorDerService;
import com.cowain.monitor.utils.PmsConstantUtils;
import com.cowain.monitor.utils.PmsProjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  
 *  * @author 冬暖
 *  * @projectName pms-monitor
 *  * @title     ErpNorDerServiceImpl   
 *  * @package    com.cowain.monitor.service.impl  
 *  * @description    物料申请单       
 *  * @date   2020/7/28 9:35  
 *  * @version V1.0.0
 *  
 */
@Service
public class ErpNorDerServiceImpl extends ServiceImpl<ErpNorDerDao, ErpNorderEntity> implements ErpNorDerService {
    @Autowired
    private PmsOrderInfoDao infoDao;
    @Autowired
    private ErpSoDao soDao;
    @Autowired
    private PmsProjectUtil pmsProjectUtil;
    /**
     * 查询物料申请单
     * @return 物料申请单
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public List<ErpNorderEntity> findListOrder(){
        return baseMapper.findListOrder(PmsConstantUtils.IS_DEL);
    }
    /**
     * 修改物料申请单
     */
    @Override
    @DataSource(name = DataSourceNames.FIRST)
    @Transactional(value = TransactionConfig.SECOND_TX,rollbackFor = Exception.class)
    public void insertInfoOrder(List<ErpNorderEntity> norderEntityList,Long type){
        List<PmsOrderInfoEntity> infoList = new ArrayList<>();
        int start = 0;
        List<PmsOrderInfoEntity> infoEntityList = new ArrayList<>();
        norderEntityList.forEach(order -> {
            Long id;
            if(order.getProjectNo().contains("-")){
                String  projectNo = order.getProjectNo().substring(start,pmsProjectUtil.getFirst()) + "" + order.getProjectNo().substring(pmsProjectUtil.getFirst() + 1);
                id = soDao.findProjectNo(projectNo);
            }else {
                id = soDao.findProjectNo(order.getProjectNo());
            }
            Long projectId = infoDao.findPmsOrderInfo(id, type);
            PmsOrderInfoEntity info = new PmsOrderInfoEntity();
            info.setProjectId(id);
            info.setOrdNum(order.getDocNo());
            info.setOrdNu(order.getNum().longValue());
            Long code = pmsProjectUtil.findCode(PmsConstantUtils.STATUS_NODER, order.getState());
            info.setOrdStatus(code);
            info.setOrdTime(null);
            info.setOrdType(type);
            info.setProjectNo(order.getProjectNo());
            if(projectId != null){
                info.setId(projectId);
                infoEntityList.add(info);
            }else {
                infoList.add(info);
            }
            order.setProjectId(id);
        });
        List<PmsOrderInfoEntity> list1 = new ArrayList<>();
        boolean flag = false;
        List<Long> list = pmsProjectUtil.addList(infoList, list1);
        if(infoList.size() > 0 && list.size() > 0){
            List<PmsOrderInfoEntity> list2= new ArrayList<>();
            infoList.forEach(info -> {
                if(info.getProjectId() == null){
                    list2.add(info);
                }
            });
            infoList.removeAll(list2);
            if(infoList.size() > 0){
                //保存
                List<List<PmsOrderInfoEntity>> split = pmsProjectUtil.split(infoList, 12);
                for (List<PmsOrderInfoEntity> orderInfoEntities : split) {
                    infoDao.insertList(orderInfoEntities);
                }
                flag = true;
            }
        }
        if(infoEntityList.size() > 0){
            if(list1.size() > 0){
                infoEntityList.addAll(list1);
            }
            List<List<PmsOrderInfoEntity>> split = pmsProjectUtil.split(infoEntityList, 12);
            for (List<PmsOrderInfoEntity> pmsOrderInfoEntities : split) {
                infoDao.updateInfoList(pmsOrderInfoEntities);
            }
        }
        if(list1.size() ==0){
            list1.addAll(infoEntityList);
        }
        if(list.size() > 0 || infoEntityList.size() > 0){
            List<PmsOrderInfoEntity> infoEntities = new ArrayList<>();
            if(flag) {
                infoEntities.addAll(infoList);
            }
            pmsProjectUtil.addLog(infoEntities,list1,PmsConstantUtils.ERP_USER,PmsConstantUtils.ERP_USER);
        }
    }
    /**
     * 修改物料申请单
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    @Transactional(value = TransactionConfig.DEFAULT_TX,rollbackFor = Exception.class)
    public void updateOrderNo(List<ErpNorderEntity> norderEntityList){
        norderEntityList.forEach(so -> {
            if(so.getProjectId() != null){
                so.setIsDel(1);
            }else {
                so.setIsDel(0);
            }
        });
        List<ErpNorderEntity> collect = norderEntityList.stream()
                .filter(s -> s.getIsDel() == 1)
                .collect(Collectors.toList());
        List<List<ErpNorderEntity>> split = pmsProjectUtil.split(collect, 12);
        for (List<ErpNorderEntity> erpNorderEntities : split) {
            baseMapper.updateOrderNo(erpNorderEntities);
        }
    }
}
